### This Script Calculates Inter-Rater Agreement scores using Rwg and merges them with the CHES data.
### It produces Figures in the Appendix and the data are used in the replication of the Ezrow et al paper.  
### Directory set and libraries loaded in Script 1. 
### 6th September 2018

rm(list=ls())

## Source in functions and data
source('Functions_Mode_Agree_Boot.R')

load('CHESData_clean.Rdata')


########################
# EU 1999 data
########################


dta1999partylabs<-unique(dta1999[,c("partyname","party.")])

agrmat.position.1999 <- agreefunc(dim=dta1999$q1,dim.name="eu_position", pid = paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,7))

agrmat.eu_salience.1999 <- agreefunc(dim=dta1999$q2,dim.name="eu_salience",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,5))

agrmat.eu_dissent.1999 <- agreefunc(dim=dta1999$q3,dim.name="eu_dissent",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,5))

agrmat.eu_future.1999 <- agreefunc(dim=dta1999$q5,dim.name="eu_future",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,5))

# value=8 (no position, missing) recoded to NA
dta1999$q6a[dta1999$q6a==8]<-NA
agrmat.eu_parliament.1999 <- agreefunc(dim=dta1999$q6a,dim.name="eu_parliament",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta1999$q6b[dta1999$q6b==8]<-NA
agrmat.eu_fiscal.1999 <- agreefunc(dim=dta1999$q6b,dim.name="eu_fiscal",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta1999$q6c[dta1999$q6c==8]<-NA
agrmat.eu_employ.1999 <- agreefunc(dim=dta1999$q6c,dim.name="eu_employ",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta1999$q6d[dta1999$q6d==8]<-NA
agrmat.eu_cohesion.1999 <- agreefunc(dim=dta1999$q6d,dim.name="eu_cohesion",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta1999$q6e[dta1999$q6e==8]<-NA
agrmat.eu_environ.1999 <- agreefunc(dim=dta1999$q6e,dim.name="eu_environ",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta1999$q6f[dta1999$q6f==8]<-NA
agrmat.eu_asylum.1999 <- agreefunc(dim=dta1999$q6f,dim.name="eu_asylum",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta1999$q6g[dta1999$q6g==8]<-NA
agrmat.eu_foreign.1999 <- agreefunc(dim=dta1999$q6g,dim.name="eu_foreign",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(1,7))

agrmat.lrgen.1999 <- agreefunc(dim=dta1999$q8a,dim.name="lrgen",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(0,10))

agrmat.lrecon.1999 <- agreefunc(dim=dta1999$q8b,dim.name="lrecon",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(0,10))

agrmat.galtan.1999 <- agreefunc(dim=dta1999$q8c,dim.name="galtan",paste(dta1999$country,dta1999$countryname,dta1999$party.,sep=","),scale=c(0,10))

agrmat.1999<- join_all(list(agrmat.eu_asylum.1999,agrmat.eu_cohesion.1999,agrmat.eu_dissent.1999,agrmat.eu_employ.1999,agrmat.eu_environ.1999,agrmat.eu_fiscal.1999,agrmat.eu_foreign.1999,agrmat.eu_future.1999,agrmat.eu_parliament.1999,agrmat.eu_salience.1999,agrmat.galtan.1999,agrmat.lrecon.1999,agrmat.lrgen.1999,agrmat.position.1999),by="party.id",type="full")

# properly code party labels

agrmat.1999<-cbind(agrmat.1999,str_split_fixed(as.character(agrmat.1999$party.id),",",3))

names(agrmat.1999)[(ncol(agrmat.1999)-2):ncol(agrmat.1999)]<-c("country","countryname","partyid")
head(agrmat.1999)

agrmat.1999 <- merge(agrmat.1999,dta1999partylabs,
	by.x="partyid",
	by.y="party.",
	all.y=T)

head(agrmat.1999)

agrmat.1999$year <- 1999



########################
# EU 2002 data
########################

dta2002[dta2002=="n"]<-NA
dta2002$q5<-as.numeric(dta2002$q5)
dta2002$q6<-as.numeric(dta2002$q6)
dta2002$q7<-as.numeric(dta2002$q7)
dta2002$q8<-as.numeric(dta2002$q8)
dta2002$q9<-as.numeric(dta2002$q9)
dta2002$q10<-as.numeric(dta2002$q10)



dta2002partylabs<-unique(dta2002[,c("party_na","party_.")])

agrmat.position.2002 <- agreefunc(dim=dta2002$q1,dim.name="eu_position",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

agrmat.eu_salience.2002 <- agreefunc(dim=dta2002$q2,dim.name="eu_salience",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,5))

agrmat.eu_dissent.2002 <- agreefunc(dim=dta2002$q12_5pt_,dim.name="eu_dissent",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,5))

agrmat.eu_dissent2.2002 <- agreefunc(dim=dta2002$q12,dim.name="eu_dissent2",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,10))

# value=8 (no position, missing) recoded to NA
dta2002$q3[dta2002$q3==8]<-NA
agrmat.eu_parliament.2002 <- agreefunc(dim=dta2002$q3,dim.name="eu_parliament",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta2002$q4[dta2002$q4==8]<-NA
agrmat.eu_intmark.2002 <- agreefunc(dim=dta2002$q4,dim.name="eu_intmark",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta2002$q5[dta2002$q5==8]<-NA
agrmat.eu_employ.2002 <- agreefunc(dim=dta2002$q5,dim.name="eu_employ",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta2002$q6[dta2002$q6==8]<-NA
agrmat.eu_agri.2002 <- agreefunc(dim=dta2002$q6,dim.name="eu_agri",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta2002$q7[dta2002$q7==8]<-NA
agrmat.eu_cohesion.2002 <- agreefunc(dim=dta2002$q7,dim.name="eu_cohesion",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta2002$q8[dta2002$q8==8]<-NA
agrmat.eu_environ.2002 <- agreefunc(dim=dta2002$q8,dim.name="eu_environ",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta2002$q9[dta2002$q9==8]<-NA
agrmat.eu_asylum.2002 <- agreefunc(dim=dta2002$q9,dim.name="eu_asylum",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta2002$q10[dta2002$q10==8]<-NA
agrmat.eu_foreign.2002 <- agreefunc(dim=dta2002$q10,dim.name="eu_foreign",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

# value=8 (no position, missing) recoded to NA
dta2002$q11[dta2002$q11==8]<-NA
agrmat.eu_enlargw.2002 <- agreefunc(dim=dta2002$q11,dim.name="eu_enlargw",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(1,7))

agrmat.lrgen.2002 <- agreefunc(dim=dta2002$q14,dim.name="lrgen",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(0,10))

agrmat.lrecon.2002 <- agreefunc(dim=dta2002$q15,dim.name="lrecon",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(0,10))

agrmat.galtan.2002 <- agreefunc(dim=dta2002$q16,dim.name="galtan",paste(dta2002$country,dta2002$countryname,dta2002$party_.,sep=","),scale=c(0,10))

agrmat.2002<- join_all(list(agrmat.eu_agri.2002,agrmat.eu_asylum.2002,agrmat.eu_cohesion.2002,agrmat.eu_dissent.2002,agrmat.eu_dissent2.2002,agrmat.eu_employ.2002,agrmat.eu_enlargw.2002,agrmat.eu_environ.2002,agrmat.eu_foreign.2002,agrmat.eu_intmark.2002,agrmat.eu_parliament.2002,agrmat.eu_salience.2002,agrmat.galtan.2002,agrmat.lrecon.2002,agrmat.lrgen.2002,agrmat.position.2002),by="party.id",type="full")

# properly code party labels

agrmat.2002<-cbind(agrmat.2002,str_split_fixed(as.character(agrmat.2002$party.id),",",3))

names(agrmat.2002)[(ncol(agrmat.2002)-2):ncol(agrmat.2002)]<-c("country","countryname","partyid")
head(agrmat.2002)

agrmat.2002 <- merge(agrmat.2002,dta2002partylabs,
	by.x="partyid",
	by.y="party_.",
	all.y=T)

head(agrmat.2002)

agrmat.2002$year <- 2002


########################
# EU 2006 data
########################


dta2006partylabs<-unique(dta2006[,c("party","party_id")])

names(dta2006)[1]<-"country"

agrmat.position.2006 <- agreefunc(dim=dta2006$Q1,dim.name="eu_position",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,7))

agrmat.eu_salience.2006 <- agreefunc(dim=dta2006$Q2,dim.name="eu_salience",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,4))

#agrmat.eu_dissent.2006 <- agreefunc(dim=dta2006$Q12_5pt_,dim.name="eu_dissent",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,5))

agrmat.eu_dissent2.2006 <- agreefunc(dim=dta2006$Q3,dim.name="eu_dissent2",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.eu_benefit.2006 <- agreefunc(dim=dta2006$Q4,dim.name="eu_benefit",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,3))


agrmat.eu_parliament.2006 <- agreefunc(dim=dta2006$Q5,dim.name="eu_parliament",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,7))


agrmat.eu_intmark.2006 <- agreefunc(dim=dta2006$Q6,dim.name="eu_intmark",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,7))


agrmat.eu_cohesion.2006 <- agreefunc(dim=dta2006$Q7,dim.name="eu_cohesion",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,7))

agrmat.eu_foreign.2006 <- agreefunc(dim=dta2006$Q8,dim.name="eu_foreign",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,7))

agrmat.eu_enlargturkey.2006 <- agreefunc(dim=dta2006$Q9,dim.name="eu_enlargturkey",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(1,7))


agrmat.lrgen.2006 <- agreefunc(dim=dta2006$Q10,dim.name="lrgen",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.lrecon.2006 <- agreefunc(dim=dta2006$Q11,dim.name="lrecon",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.galtan.2006 <- agreefunc(dim=dta2006$Q12,dim.name="galtan",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.taxspending.2006 <- agreefunc(dim=dta2006$Q13,dim.name="taxspending",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.taxspending_sal.2006 <- agreefunc(dim=dta2006$Q14,dim.name="taxspending_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.deregulation.2006 <- agreefunc(dim=dta2006$Q15,dim.name="deregulation",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.deregulation_sal.2006 <- agreefunc(dim=dta2006$Q16,dim.name="deregulation_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.redistribution.2006 <- agreefunc(dim=dta2006$Q17,dim.name="redistribution",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.redistribution_sal.2006 <- agreefunc(dim=dta2006$Q18,dim.name="redistribution_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.civillib.2006 <- agreefunc(dim=dta2006$Q19,dim.name="civillib",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.civillib_sal.2006 <- agreefunc(dim=dta2006$Q20,dim.name="civillib_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))


agrmat.civillib.2006 <- agreefunc(dim=dta2006$Q19,dim.name="civillib",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.civillib_sal.2006 <- agreefunc(dim=dta2006$Q20,dim.name="civillib_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))


agrmat.soclifestyle.2006 <- agreefunc(dim=dta2006$Q21,dim.name="soclifestyle",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.soclifestyle_sal.2006 <- agreefunc(dim=dta2006$Q22,dim.name="soclifestyle_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.religpolitics.2006 <- agreefunc(dim=dta2006$Q23,dim.name="religpolitics",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.religpolitics_sal.2006 <- agreefunc(dim=dta2006$Q24,dim.name="religpolitics_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.immigration.2006 <- agreefunc(dim=dta2006$Q25,dim.name="religpolitics",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.immigration_sal.2006 <- agreefunc(dim=dta2006$Q26,dim.name="religpolitics_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.integrationasylum.2006 <- agreefunc(dim=dta2006$Q27,dim.name="integrationasylum",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.integrationasylm_sal.2006 <- agreefunc(dim=dta2006$Q28,dim.name="integrationasylum_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.urbanrural.2006 <- agreefunc(dim=dta2006$Q29,dim.name="urbanrural",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.urbanrural_sal.2006 <- agreefunc(dim=dta2006$Q30,dim.name="urbanrural_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.cosmopolitan.2006 <- agreefunc(dim=dta2006$Q31,dim.name="cosmo",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.cosmopolitan_sal.2006 <- agreefunc(dim=dta2006$Q32,dim.name="cosmo_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.decentralization.2006 <- agreefunc(dim=dta2006$Q33,dim.name="decentralization",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.decentralization_sal.2006 <- agreefunc(dim=dta2006$Q34,dim.name="decentralization_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.usapower.2006 <- agreefunc(dim=dta2006$Q35,dim.name="usapower",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.usapower_sal.2006 <- agreefunc(dim=dta2006$Q36,dim.name="usapower_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.ethnicminorities.2006 <- agreefunc(dim=dta2006$Q37,dim.name="ethnicminorities",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.ethniceminorities_sal.2006 <- agreefunc(dim=dta2006$Q38,dim.name="ethniceminorities_sal",paste(dta2006$country,dta2006$countryname,dta2006$party_id,sep=","),scale=c(0,10))

agrmat.2006<- join_all(list(agrmat.civillib_sal.2006,agrmat.civillib.2006,agrmat.cosmopolitan_sal.2006,agrmat.cosmopolitan.2006,agrmat.decentralization_sal.2006,agrmat.decentralization.2006,agrmat.deregulation_sal.2006,agrmat.deregulation.2006,agrmat.ethniceminorities_sal.2006,agrmat.ethnicminorities.2006,agrmat.eu_benefit.2006,agrmat.eu_cohesion.2006,agrmat.eu_dissent2.2006,agrmat.eu_enlargturkey.2006,agrmat.eu_foreign.2006,agrmat.eu_intmark.2006,agrmat.eu_parliament.2006,agrmat.eu_salience.2006,agrmat.galtan.2006,agrmat.immigration_sal.2006,agrmat.immigration.2006,agrmat.integrationasylm_sal.2006,agrmat.integrationasylum.2006,agrmat.lrecon.2006,agrmat.lrgen.2006,agrmat.position.2006,agrmat.redistribution_sal.2006,agrmat.redistribution.2006,agrmat.religpolitics_sal.2006,agrmat.religpolitics.2006,agrmat.soclifestyle_sal.2006,agrmat.soclifestyle.2006,agrmat.taxspending_sal.2006,agrmat.taxspending.2006,agrmat.urbanrural_sal.2006,agrmat.urbanrural.2006,agrmat.usapower_sal.2006,agrmat.usapower.2006),by="party.id",type="full")

# properly code party labels

agrmat.2006<-cbind(agrmat.2006,str_split_fixed(as.character(agrmat.2006$party.id),",",3))

names(agrmat.2006)[(ncol(agrmat.2006)-2):ncol(agrmat.2006)]<-c("country","countryname","partyid")
head(agrmat.2006)

agrmat.2006 <- merge(agrmat.2006,dta2006partylabs,
	by.x="partyid",
	by.y="party_id",
	all.y=T)

head(agrmat.2006)

agrmat.2006$year <- 2006


### 2010 data


# EU Position 
agrmat.2010.position <- agreefunc(dim=dta2010[,6],dim.name="eu_position",paste(dta2010$country,dta2010$countryname,dta2010$party_id,sep=","),scale=c(1,7))

# EU Salience 
agrmat.2010.eu_sal <- agreefunc(dim=dta2010[,7],dim.name="eu_salience",paste(dta2010$country,dta2010$countryname,dta2010$party_id,sep=","),scale=c(0,4))

#EU Dissent	
agrmat.2010.eu_dissent <- agreefunc(dim=dta2010[,8],dim.name="eu_dissent",paste(dta2010$country,dta2010$countryname,dta2010$party_id,sep=","),scale=c(0,10))

# Run for dimensions on 1-7 scale

for (i in 10:14){
	names <- colnames(dta2010)[i]
	test <- agreefunc(dim=dta2010[,i],dim.name=names,paste(dta2010$country,dta2010$countryname,dta2010$party_id,sep=","),scale=c(1,7))
	nm <- paste("agrmat.2010",names,sep=".")
	assign(nm,test)
}

# Run for dimensions on 0-10 scale

for (i in 15:43){
	names <- colnames(dta2010)[i]
	test <- agreefunc(dim=dta2010[,i],dim.name=names,paste(dta2010$country,dta2010$countryname,dta2010$party_id,sep=","),scale=c(0,10))
	nm <- paste("agrmat.2010",names,sep=".")
	assign(nm,test)
}

agrmat.2010<- join_all(list(agrmat.2010.civlib_laworder,agrmat.2010.civlib_salience, agrmat.2010.dereg_salience, agrmat.2010.deregulation, agrmat.2010.enviro_salience, agrmat.2010.environment, agrmat.2010.ethnic_minorities, agrmat.2010.ethnic_salience, agrmat.2010.eu_cohesion, agrmat.2010.eu_dissent,agrmat.2010.eu_ep,agrmat.2010.eu_foreign,agrmat.2010.eu_intmark,agrmat.2010.eu_sal,agrmat.2010.eu_turkey,agrmat.2010.galtan,agrmat.2010.immigra_salience,agrmat.2010.immigrate_policy,agrmat.2010.international_salience,agrmat.2010.international_security,agrmat.2010.lrecon,agrmat.2010.lrgen,agrmat.2010.multicult_salience,agrmat.2010.multiculturalism,agrmat.2010.position,agrmat.2010.redist_salience,agrmat.2010.redistribution,agrmat.2010.region_salience,agrmat.2010.regions,agrmat.2010.relig_salience,agrmat.2010.religious_principle,agrmat.2010.social_salience,agrmat.2010.sociallifestyle,agrmat.2010.spendvtax,agrmat.2010.spendvtax_salience,agrmat.2010.urban_rural,agrmat.2010.urban_salience),by="party.id",type="full")

# properly code party labels

agrmat.2010<-cbind(agrmat.2010,str_split_fixed(as.character(agrmat.2010$party.id),",",3))

names(agrmat.2010)[(ncol(agrmat.2010)-2):ncol(agrmat.2010)]<-c("country","countryname","partyid")
head(agrmat.2010)

dta2010partylabs<-unique(dta2010[,c("party_name","party_id")])

agrmat.2010 <- merge(agrmat.2010,dta2010partylabs,
	by.x="partyid",
	by.y="party_id",
	all.y=T)

head(agrmat.2010)

agrmat.2010$year <- 2010


# combine and save all surveys

final<-list(agrmat.1999,agrmat.2002,agrmat.2006,agrmat.2010)
save(final,file="CHES_RWG_1999_2010_alldimensions.Rda")


###############################
###### Begin Plots for 2010 
##### This produces Figures A1 and A2 in Appendix 

agrmat.rwg <- as.data.frame(cbind(agrmat.2010$party.id, agrmat.2010$numresp, agrmat.2010[,grep(".rwg",names(agrmat.2010),fixed=T)]))
names(agrmat.rwg)[1] <- "party_id"
names(agrmat.rwg)[2] <- "numresp"

testgood<-agrmat.rwg[,agrep(".rwg",names(agrmat.rwg))]>0.7
testbad<-agrmat.rwg[,agrep(".rwg",names(agrmat.rwg))]<0.5
mean(testgood,na.rm=T)
mean(testbad,na.rm=T)


agrmat.r <- agrmat.rwg[,3:39]

t <- agrmat.r[,order(miscTools::colMedians(agrmat.r,na.rm=T))]

colnames(t) <- gsub(".rwg","",colnames(t))

pdf("RWGbyDim.pdf",width=9,height=12)
par(mar=c(4,12,4,2))
plot(1, type="n", ylim=c(0.5,38.5), xlim=c(0,1), xlab="", ylab="", axes=FALSE)
grid(ny=NA,col="grey")
boxplot(t,horizontal=T,las=1,col="grey",add=T,main="Agreement Score Rwg \n(sorted by median)")
abline(v=0.7,col="red",lty=2,lwd=2)
abline(v=0.5,col="red",lty=2,lwd=2)

dev.off()

## By party


agrmat.rwg$party_id <- as.numeric(sapply(strsplit(as.character(agrmat.rwg$party_id), split = ","),"[[",3))
pnames <- as.data.frame(unique(dta2010[,c(1,2,4,5)]))

agrmat.rwg.names <- merge(agrmat.rwg,pnames,by="party_id")
name <- paste(agrmat.rwg.names$cname,agrmat.rwg.names$party_name,sep=":")
agrmat.r <- t(as.matrix(agrmat.rwg.names[,3:39]))
colnames(agrmat.r) <- name
agrmat.r <- as.data.frame(agrmat.r)

tp <- agrmat.r[,order(miscTools::colMedians(agrmat.r,na.rm=T))]

pdf("RWGbyParty.pdf",width=9,height=12)
par(mar=c(4,7,4,2))
plot(1, type="n", ylim=c(0,ncol(tp)), xlim=c(0,1), xlab="", ylab="", axes=FALSE)
grid(ny=NA,col="grey")
boxplot(tp,horizontal=T,las=1,col="grey",add=T,main="Agreement Score Rwg \n(sorted by median)", yaxt='n',outline=F)
abline(v=0.7,col="red",lty=2,lwd=2)
abline(v=0.5,col="red",lty=2,lwd=2)

dev.off()

## Subset parties 

tp.1 <- tp[,miscTools::colMedians(tp,na.rm=T)>0.75]

pdf("RWGbyParty_best.pdf",width=9,height=12)
par(mar=c(4,7,4,2))
plot(1, type="n", ylim=c(0,ncol(tp.1)), xlim=c(0,1), xlab="", ylab="", axes=FALSE)
grid(ny=NA,col="grey")
boxplot(tp.1,horizontal=T,las=1,col="grey",add=T,main="Agreement Score Rwg \n(sorted by median)")
abline(v=0.7,col="red",lty=2,lwd=2)
abline(v=0.5,col="red",lty=2,lwd=2)
dev.off()

tp.2 <- tp[,miscTools::colMedians(tp,na.rm=T)<0.60]
pdf("RWGbyParty_worst.pdf",width=9,height=12)
par(mar=c(4,7,4,2))
plot(1, type="n", ylim=c(0,ncol(tp.2)), xlim=c(0,1), xlab="", ylab="", axes=FALSE)
grid(ny=NA,col="grey")
boxplot(tp.2,horizontal=T,las=1,col="grey",add=T,main="Agreement Score Rwg \n(sorted by median)")
abline(v=0.7,col="red",lty=2,lwd=2)
abline(v=0.5,col="red",lty=2,lwd=2)

dev.off()

#############################################
############################################
### Appendix Figure A4 --triangular reference distribution

### 1999
agrmat.1999.long <- reshape2::melt(agrmat.1999, id.vars = c("partyid","party.id","country","countryname","partyname","year"), variable_name = "rwg")

agrmat.1999.rwgtriang<-agrmat.1999.long[grep("triang",agrmat.1999.long$variable),]
agrmat.1999.rwgtriang$sd<-agrmat.1999.long[grep("\\.sd",agrmat.1999.long$variable),"value"]
agrmat.1999.rwgtriang$dim<-str_split_fixed(agrmat.1999.rwgtriang$variable,"\\.",3)[,1]
agrmat.1999.rwgtriang<-na.omit(agrmat.1999.rwgtriang)
agrmat.1999.rwgtriang$rwg<-agrmat.1999.rwgtriang$variable
### 2002
agrmat.2002.long <- reshape2::melt(agrmat.2002, id.vars = c("partyid","party.id","country","countryname","party_na","year"),
  variable_name = "rwg")
agrmat.2002.long<-na.omit(agrmat.2002.long)

agrmat.2002.rwgtriang<-agrmat.2002.long[grep("triang",agrmat.2002.long$variable),]
agrmat.2002.rwgtriang$dim<-str_split_fixed(agrmat.2002.rwgtriang$variable,"\\.",3)[,1]
agrmat.2002.rwgtriang<-na.omit(agrmat.2002.rwgtriang)
agrmat.2002.rwgtriang$rwg<-agrmat.2002.rwgtriang$variable

### 2006
agrmat.2006.long <- reshape2::melt(agrmat.2006, id.vars = c("partyid","party.id","country","countryname","party","year"),
  variable_name = "rwg")


agrmat.2006.long<-na.omit(agrmat.2006.long)

agrmat.2006.rwgtriang<-agrmat.2006.long[grep("triang",agrmat.2006.long$variable),]
agrmat.2006.rwgtriang$dim<-str_split_fixed(agrmat.2006.rwgtriang$variable,"\\.",3)[,1]
agrmat.2006.rwgtriang<-na.omit(agrmat.2006.rwgtriang)
agrmat.2006.rwgtriang$rwg<-agrmat.2006.rwgtriang$variable
### 2010
agrmat.2010.long <- reshape2::melt(agrmat.2010, id.vars = c("partyid","party.id","country","countryname","party_name","year"),
  variable_name = "rwg")
agrmat.2010.long<-na.omit(agrmat.2010.long)

agrmat.2010.rwgtriang<-agrmat.2010.long[grep("triang",agrmat.2010.long$variable),]
agrmat.2010.rwgtriang$dim<-str_split_fixed(agrmat.2010.rwgtriang$variable,"\\.",3)[,1]
agrmat.2010.rwgtriang<-na.omit(agrmat.2010.rwgtriang)
agrmat.2010.rwgtriang$rwg<-agrmat.2010.rwgtriang$variable

### Which dimensions are poorly measured over time?


dim1999 <- agrmat.1999.rwgtriang %>%
  group_by(dim) %>%
  dplyr::summarise(
    mean_rwg = mean(as.numeric(value), na.rm=T))

names(dim1999)[2]<-"1999"

dim2002 <- agrmat.2002.rwgtriang %>%
  group_by(dim) %>%
  dplyr::summarise(
    mean_rwg = mean(as.numeric(value), na.rm=T))


names(dim2002)[2]<-"2002"

dim2006 <- agrmat.2006.rwgtriang %>%
  group_by(dim) %>%
  dplyr::summarise(
    mean_rwg = mean(as.numeric(value), na.rm=T))
names(dim2006)[2]<-"2006"

dim2010 <- agrmat.2010.rwgtriang %>%
  group_by(dim) %>%
  dplyr::summarise(
    mean_rwg = mean(as.numeric(value), na.rm=T))
names(dim2010)[2]<-"2010"

rwgtrialldim<-join_all(list(dim1999,dim2002,dim2006,dim2010),by="dim",type="full")

rwgtrialldimImportant<-(na.omit(rwgtrialldim))

rwgtrialldimlong<-reshape2::melt(rwgtrialldimImportant)

mycols<-brewer.pal(4, "Set1")




pdf("rwgtime.pdf",width=8,height=6)
dotplot(  main="Expert Agreement for Selected Scales Over Time", rwgtrialldimlong$dim ~ rwgtrialldimlong$value,     group=rwgtrialldimlong$variable, xlim=c(0.35,1.05),scales=list(
    x=list(
      at=seq(.4,1,.1)
       )
    ),

xlab="Mean RWG Score (null dist: triangular)", key=list(space="top", columns=2,title="Survey Year",pch=19,cex.title=1,points = list(pch = 19, col = mycols), text=list(c("1999","2002","2006","2010"))),pch=19,cex=1.1, col=mycols,panel=function(x,y,...){
             panel.dotplot(x,y,...)
             labs <- rwgtrialldimlong[list(...)$subscripts,]$variable
			       panel.abline(v=0,lty="dotted",col="grey") 
})
dev.off()

### Which parties are poorly measured over time?
# party ids are different for 1999, cannot be merged easily

dim2002party <- agrmat.2002.rwgtriang %>%
  group_by(partyid, party.id, countryname) %>%
  dplyr::summarise(
    mean_rwg = mean(as.numeric(value), na.rm=T))
names(dim2002party)[4]<-"2002"
dim2002party <- dim2002party[,c(1,4)]

dim2006party <- agrmat.2006.rwgtriang %>%
  group_by(partyid, party.id, countryname) %>%
  dplyr::summarise(
    mean_rwg = mean(as.numeric(value), na.rm=T))
names(dim2006party)[4]<-"2006"
dim2006party <- dim2006party[,c(1,4)]

dim2010party <- agrmat.2010.rwgtriang %>%
  group_by(partyid, party.id, countryname) %>%
  dplyr::summarise(
    mean_rwg = mean(as.numeric(value), na.rm=T))
names(dim2010party)[4]<-"2010"
dim2010party <- dim2010party[,c(1,4)]


#dta2010partylabs2<-dta2010partylabs
#names(dta2010partylabs2)[2]<-"partyid"

rwgtrialldimparty<-join_all(list(dim2002party,dim2006party,dim2010party),by="partyid",type="full")

rwgtrialldimparty2010<-rwgtrialldimparty[!is.na(rwgtrialldimparty$"2010"),]

rwgtrialldimparty<-(na.omit(rwgtrialldimparty))

dim(rwgtrialldimparty)

rwgtrialldimparty$sd<-apply(rwgtrialldimparty[,2:4],1,sd)

# plot parties with the 10 highest rwg sds over time (2002-2010)
topten <- rwgtrialldimparty[order(rwgtrialldimparty$sd, decreasing=T),]
topten <- topten[1:10,]

topten<-reshape2::melt(topten)[1:30,]

partyaux <- unique(agrmat.2010.rwgtriang[,c("partyid","countryname","party_name")])

topten2 <- left_join(topten, partyaux, by="partyid")

topten2$variable<-factor(topten2$variable)

pdf("rwgparties.pdf",width=8,height=6)
dotplot(  main="Parties with high expert agreement variability over time" , 
          paste(topten2$party_name, topten2$countryname,sep=", ")~ topten2$value,
          group= topten2$variable,xlim=c(0.3,1.05),scales=list(
    x=list(
      at=seq(0.4,1,.1)
       )
    ),

xlab="Mean RWG score (null dist: triangular)", 

key=list(space="top", columns=2, title="Survey Year", pch=19, cex.title=1, points = list(pch = 19, col = mycols[1:3]),     
         text=list(c("2002","2006","2010"))),
 pch=19,cex=1.1,
  col=mycols[1:3],          
           panel=function(x,y,...){
                 panel.dotplot(x,y,...)
                labs <- rwgtrialldimlong[list(...)$subscripts,]$variable
			          panel.abline(v=0,lty="dotted",col="grey") 
})
dev.off()

########################
# Appendix Figure A3 -- best and worst parties over time
bestworst <- left_join(rwgtrialldimparty, partyaux, by="partyid")

bestworst<-reshape2::melt(bestworst)

bestworst <- bestworst[bestworst$variable != "sd" , ]

bestworst1 <- bestworst %>%
  group_by(partyid, countryname, party_name, variable) %>%
  dplyr::summarise(
    mean_rwg = mean(as.numeric(value), na.rm=T))

# best 5% and worst 5% of parties
bestparties <- bestworst1[bestworst1$mean_rwg >  quantile(bestworst1$mean_rwg,0.95),]
bestparties<-bestparties[order(bestparties$mean_rwg),]
worstparties<-bestworst1[bestworst1$mean_rwg < quantile(bestworst1$mean_rwg,0.05) ,]
worstparties<-worstparties[order(worstparties$mean_rwg),]

pdf("rwgbestworst.pdf",width=15,height=6)
par(mfrow=c(1,2))
dotchart(  main="Parties with highest expert agreement\n(2002-2010)",labels=paste(bestparties$party_name, bestparties$countryname,bestparties$variable,sep=", ") ,x=bestparties$mean_rwg,xlim=c(0.2,0.9),pch=19)

dotchart(  main="Parties with lowest expert agreement\n(2002-2010)",labels=paste(worstparties$party_name, worstparties$countryname,worstparties$variable,sep=", ") ,x=worstparties$mean_rwg,xlim=c(0.2,0.9),pch=19)
dev.off()